home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9329 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: detroit.freenet.org!ab411
  2. From: ab411@detroit.freenet.org (David R. Conrad)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: 6 byte real to double; How?
  5. Date: 9 Mar 1996 13:11:36 GMT
  6. Organization: Greater Detroit Free-Net, Detroit, MI
  7. Message-ID: <4hs028$apu@detroit.freenet.org>
  8. References: <4gg88a$3oi@aphex.direct.ca> <3128306A.21F@hsc.unt.edu>
  9. Reply-To: ab411@detroit.freenet.org (David R. Conrad)
  10. NNTP-Posting-Host: detroit.freenet.org
  11.  
  12.  
  13. >In article <3128306A.21F@hsc.unt.edu>, sfogoros@hsc.unt.edu says...
  14. >>
  15. >>I have a lot of data files that were created by a pascal program. The 
  16. >>data is stored as 'file of real' meaning that each floating point value 
  17. >>is stored in six bytes. I need to access this data from a c program 
  18. >>(borland). I would like any assistance you could provide. I guess what 
  19. >>I need is a canned function that would take a 6 byte string (actually the 
  20. >>real), and return a double.
  21.  
  22. I'm going to go out on a limb (okay, so it's a pretty short limb) and
  23. assume that this file was created with Turbo Pascal.  The format of the
  24. TP real data type, according to my TP 5.5 Reference Guide:
  25.  
  26. # A 6-byte (48-bit) Real number is divided into three fields:
  27. #
  28. #   1         39           8         width
  29. # +---+---------------+---------+
  30. # | s |       f       |    e    |
  31. # +---+---------------+---------+
  32. #      msb         lsb msb   lsb     order
  33. #
  34. # The value v of the number is determined by
  35. #
  36. # if 0 < e <= 255, then v = (-1)^s * 2^(e-129) * (1.f).
  37. # if e = 0, then v = 0.
  38. #
  39. # Note: The real type cannot store denormals, NaNs, and infinities.
  40. # Denormals become zero when stored in a real, and NaNs and infinities
  41. # produce an overflow error if an attempt is made to store them in a real.
  42.  
  43. Note that in the first line of the formula for v they had "s" and "(e-129)"
  44. as actual superscripts, which I've indicated with carats (^).
  45.  
  46. In other words, a TP real has a sign bit, 39 mantissa bits with an implied
  47. one bit in front of them, and an 8 bit exponent in "excess 129" notation.
  48.  
  49. I assume that between the diagram above, my explanation, and the
  50. pseudo-code they gave for determining the value, you'll have no trouble
  51. writing your C function.  You probably would have gotten an answer
  52. quicker by posting to comp.lang.pascal.
  53.  
  54. -- 
  55. David R. Conrad, conrad@detroit.freenet.org   PGP key on    GDFN Hardware and
  56. http://detroit.freenet.org/staff/conrad        home page   Software Committee
  57. "If you can't say 'fuck', you can't say 'fuck the government'." --Lenny Bruce
  58. "We had to destroy the Global Electronic Village in order to save it." --Exon
  59.